package com.yinhe.music.yhmusic.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.apkfuns.logutils.LogUtils;
import com.dou361.dialogui.DialogUIUtils;
import com.dou361.dialogui.bean.BuildBean;
import com.hwangjr.rxbus.RxBus;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BasePresneterActivity;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.login.phonelogin.LoginActivity;
import com.yinhe.music.yhmusic.login.phonelogin.LoginContract;
import com.yinhe.music.yhmusic.login.phonelogin.LoginPresenter;
import com.yinhe.music.yhmusic.login.validcode.RegisterActivity;
import com.yinhe.music.yhmusic.model.LoginInfo;
import com.yinhe.music.yhmusic.utils.UmengEventUtils;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LoginRegisterActivity extends BasePresneterActivity implements LoginContract.ILoginView {
    @BindView(R.id.cancel)
    TextView cancel;
    @BindView(R.id.login_btn)
    Button login;
    @BindView(R.id.register)
    Button register;
    @BindView(R.id.login_visitor)
    TextView visitorLogin;
    @BindView(R.id.qq_login)
    LinearLayout qqLogin;
    @BindView(R.id.wechat_login)
    LinearLayout wechatLogin;
    @BindView(R.id.sina_login)
    LinearLayout sinaLogin;
    private UMShareAPI umShareAPI;
    private BuildBean dialog;

    LoginPresenter mPresenter;

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new LoginPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        RxBus.get().register(this);
        umShareAPI = UMShareAPI.get(LoginRegisterActivity.this);
        dialog = DialogUIUtils.showLoading(this, "正在登录。。。", true, true, false, false);
        setListener();
    }


    private void setListener() {
        // 登录
        login.setOnClickListener((v) -> {
            Intent intent = new Intent(LoginRegisterActivity.this, LoginActivity.class);
            intent.putExtra("type", IConstants.LOGIN_FRAGMENT);
            startActivity(intent);
            finish();
        });

        // 注册
        register.setOnClickListener((v) -> {
            MobclickAgent.onEvent(LoginRegisterActivity.this, "registration_results");
            Intent intent = new Intent(LoginRegisterActivity.this, RegisterActivity.class);
            startActivity(intent);
        });

        // 返回
        cancel.setOnClickListener((v) -> onBackPressed());

        // 新浪登录
        sinaLogin.setOnClickListener((v) -> {
            dialog.show();
            umShareAPI.getPlatformInfo(LoginRegisterActivity.this, SHARE_MEDIA.SINA, umAuthListener);
        });

        // QQ登录
        qqLogin.setOnClickListener((v) -> {
            dialog.show();
            umShareAPI.getPlatformInfo(LoginRegisterActivity.this, SHARE_MEDIA.QQ, umAuthListener);
        });

        // 微信登录
        wechatLogin.setOnClickListener((v) -> {
            dialog.show();
            umShareAPI.getPlatformInfo(LoginRegisterActivity.this, SHARE_MEDIA.WEIXIN, umAuthListener);
        });

        // 游客
        visitorLogin.setOnClickListener((v) -> finish());
    }

    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            //授权开始的回调
        }

        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            String platformId = data.get("uid");
            String name       = data.get("name");
            String gender     = data.get("gender");
            String iconimage  = data.get("iconurl");

            getLoginPlf(platform);
            mPresenter.thirdPartLogin(platform == SHARE_MEDIA.QQ ? data.get("unionid") : null,
                    platformId, name, gender, iconimage, String.valueOf(platform));
        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            DialogUIUtils.dismiss(dialog);
            Toast.makeText(getApplicationContext(), "登录失败：" + t.getMessage(), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            DialogUIUtils.dismiss(dialog);
            Toast.makeText(getApplicationContext(), "取消登录", Toast.LENGTH_SHORT).show();
        }
    };

    private void getLoginPlf(SHARE_MEDIA platform) {
        switch (platform) {
            case QQ:
                UmengEventUtils.login(this, UmengEventUtils.QQ);
                break;
            case SINA:
                UmengEventUtils.login(this, UmengEventUtils.SINA);
                break;
            case WEIXIN:
                UmengEventUtils.login(this, UmengEventUtils.WECHAT);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        DialogUIUtils.dismiss(dialog);
        RxBus.get().unregister(this);
        super.onDestroy();
    }

    @Override
    public void showMessage(Throwable throwable) {
        super.showMessage(throwable);
        DialogUIUtils.dismiss(dialog);
    }

    @Override
    public void setLogin(LoginInfo loginInfo) {
        DialogUIUtils.dismiss(dialog);
        mPresenter.saveLoginInfo(loginInfo);
        finish();
    }
}